package com.example.test;

import static android.content.ContentValues.TAG;
import static com.qweather.sdk.bean.base.Lang.ZH_HANS;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.qweather.sdk.bean.base.Code;
import com.qweather.sdk.bean.base.Unit;
import com.qweather.sdk.bean.geo.GeoBean;
import com.qweather.sdk.bean.warning.WarningBean;
import com.qweather.sdk.bean.weather.WeatherDailyBean;
import com.qweather.sdk.bean.weather.WeatherNowBean;
import com.qweather.sdk.view.HeConfig;
import com.qweather.sdk.view.QWeather;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    TextView weather,cs,wd,wc,day1,day2,day3,yj;
    EditText city;
    Button search;
    String tq,cityname,cityid,shuru,stwd,stfx,stfl,stgw,stdw,stdate1,stdate2,
            stdate3,stgw2,stgw3,stdw2,stdw3,tq2,tq3,stfx2,stfx3,styj=null;
    private Handler mHandler=new Handler(){
      public void  handleMessage(Message msg){
          super.handleMessage(msg);
          if (msg.what==1){
              cs.setText(msg.getData().getString("cityname"));
              weather.setText(msg.getData().getString("tq")+"  "
                      +msg.getData().getString("fx")
                      +msg.getData().getString("fl")+"级");
              wd.setText(msg.getData().getString("wd")+"℃");
              wc.setText(msg.getData().getString("gw")+"℃"+"/"+
                      msg.getData().getString("dw")+"℃");
              day1.setText(msg.getData().getString("date1")+"   "+
                      msg.getData().getString("tq")+"  "+
                      msg.getData().getString("fx")+"  "+
                      msg.getData().getString("gw")+"℃"+"/"+
                      msg.getData().getString("dw")+"℃");
              day2.setText(msg.getData().getString("date2")+"   "+
                      msg.getData().getString("tq2")+"  "+
                      msg.getData().getString("fx2")+"  "+
                      msg.getData().getString("gw2")+"℃"+"/"+
                      msg.getData().getString("dw2")+"℃");
              day3.setText(msg.getData().getString("date3")+"   "+
                      msg.getData().getString("tq3")+"  "+
                      msg.getData().getString("fx3")+"  "+
                      msg.getData().getString("gw3")+"℃"+"/"+
                      msg.getData().getString("dw3")+"℃");

              yj.setText(msg.getData().getString("yj"));
          }
      }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        weather=(TextView)findViewById(R.id.tv_weather);
        cs=(TextView)findViewById(R.id.tv_cs);
        wd=(TextView)findViewById(R.id.tv_wd);
        search=(Button)findViewById(R.id.btn_search);
        city=(EditText) findViewById(R.id.et_city);
        wc=(TextView)findViewById(R.id.tv_wencha);
        day1=(TextView)findViewById(R.id.tv_day1);
        day2=(TextView)findViewById(R.id.tv_day2);
        day3=(TextView)findViewById(R.id.tv_day3);
        yj=(TextView)findViewById(R.id.tv_yujing);
        HeConfig.init("HE2305150817451652", "9be4215a5d81443fb7b3e26179c5654f");
        HeConfig.switchToDevService();

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        shuru=city.getText().toString().trim();
                        //获得城市编码
                        QWeather.getGeoCityLookup(MainActivity.this, shuru, new QWeather.OnResultGeoListener() {
                            @Override
                            public void onError(Throwable throwable) {}

                            @Override
                            public void onSuccess(GeoBean geoBean) {
                                List<GeoBean.LocationBean> locationBeans=geoBean.getLocationBean();
                                cityid=locationBeans.get(0).getId();
                                cityname=locationBeans.get(0).getName();
                                System.out.println(cityname);
                            }
                        });
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        //获取实时天气
                        QWeather.getWeatherNow(MainActivity.this, cityid, ZH_HANS, Unit.METRIC, new QWeather.OnResultWeatherNowListener() {
                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onSuccess(WeatherNowBean weatherBean) {
                                Log.i(TAG, "getWeather onSuccess: " + new Gson().toJson(weatherBean));
                                //先判断返回的status是否正确，当status正确时获取数据，若status不正确，可查看status对应的Code值找到原因
                                if (Code.OK == weatherBean.getCode()) {
                                    WeatherNowBean.NowBaseBean now = weatherBean.getNow();
                                    tq=now.getText();
                                    stwd=now.getTemp();
                                    stfx=now.getWindDir();
                                    stfl=now.getWindScale();

                                } else {
                                    //在此查看返回数据失败的原因
                                    Code code = weatherBean.getCode();
                                    Log.i(TAG, "failed code: " + code);
                                }
                            }
                        });
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        //获取三天天气
                        QWeather.getWeather3D(MainActivity.this, cityid, new QWeather.OnResultWeatherDailyListener() {
                            @Override
                            public void onError(Throwable throwable) {
                            }

                            @Override
                            public void onSuccess(WeatherDailyBean weatherDailyBean) {
                                List<WeatherDailyBean.DailyBean> dailyBeans=weatherDailyBean.getDaily();
                                stgw=dailyBeans.get(0).getTempMax();
                                stdw=dailyBeans.get(0).getTempMin();
                                stdate1=dailyBeans.get(0).getFxDate();
                                stdate2=dailyBeans.get(1).getFxDate();
                                stdate3=dailyBeans.get(2).getFxDate();
                                tq2=dailyBeans.get(1).getTextDay();
                                tq3=dailyBeans.get(2).getTextDay();
                                stgw2=dailyBeans.get(1).getTempMax();
                                stgw3=dailyBeans.get(0).getTempMax();
                                stdw2=dailyBeans.get(1).getTempMin();
                                stdw3=dailyBeans.get(2).getTempMin();
                                stfx=dailyBeans.get(0).getWindDirDay();
                                stfx2=dailyBeans.get(1).getWindDirDay();
                                stfx3=dailyBeans.get(2).getWindDirDay();
                            }
                        });
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        //获取预警信息
                        QWeather.getWarning(MainActivity.this, cityid, new QWeather.OnResultWarningListener() {
                            @Override
                            public void onError(Throwable throwable) {
                            }

                            @Override
                            public void onSuccess(WarningBean warningBean) {
                                List<WarningBean.WarningBeanBase> warningBeanBases=warningBean.getWarningList();
                                if (!warningBeanBases.isEmpty()){
                                    styj=warningBeanBases.get(0).getText();
                                }
                                System.out.println(styj);
                            }
                        });
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        if (tq!=null&cityname!=null)
                        {
                            Message message=Message.obtain();
                            message.what=1;
                            Bundle bundle=new Bundle();
                            bundle.putString("tq",tq);
                            bundle.putString("cityname",cityname);
                            bundle.putString("wd",stwd);
                            bundle.putString("fl",stfl);
                            bundle.putString("fx",stfx);
                            bundle.putString("gw",stgw);
                            bundle.putString("dw",stdw);
                            bundle.putString("date1",stdate1);
                            bundle.putString("date2",stdate2);
                            bundle.putString("date3",stdate3);
                            bundle.putString("tq2",tq2);
                            bundle.putString("tq3",tq3);
                            bundle.putString("gw2",stgw2);
                            bundle.putString("gw3",stgw3);
                            bundle.putString("dw2",stdw2);
                            bundle.putString("dw3",stdw3);
                            bundle.putString("fx2",stfx2);
                            bundle.putString("fx3",stfx3);
                            bundle.putString("yj",styj);
                            message.setData(bundle);
                            mHandler.sendMessage(message);
                        }
                    }
                }).start();
            }
        });

    }
}